﻿package net.wangit.framework.util;

import net.wangit.Ajax;
import net.wangit.context.ActionContext;
import net.wangit.support.Action;
import net.wangit.support.ElementWriter;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.component.SelectElementWriter;
import net.wangit.view.enumeration.EnumerationFactory;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.unit.UnitFactory;

public class LinkSelectAction implements Action {

	private static final ElementWriter writer = new SelectElementWriter();

	public LinkSelectAction() {
	}

	public ActionContext execute(ActionContext ac) throws Exception {
		String domain = ac.getRequestParameter("domain").trim();
		String id = ac.getRequestParameter("id");
		int index = id.lastIndexOf(".");
		String unitId = id.substring(0, index).trim();
		String elementId = id.substring(index + 1).trim();
		UnitComponent uc = UnitFactory.getUnitWithoutClone(domain, unitId);
		ElementComponent ec = uc.getElementComponent(elementId).cloneElement(
				ac, null);
		int showMode = ac.getRequestParameterInt((new StringBuilder(String
				.valueOf(id))).append(".42.m").toString(), 3);
		ec.setShowMode(showMode);
		String enumerationId = (String) ec.getObject();
		if (!Checker.isEmpty(enumerationId))
			ec.setObject(EnumerationFactory.getEnumeration(ac, domain,
					enumerationId));
		Ajax ajax = new Ajax(ac);
		StringBuffer html = new StringBuffer();
		writer.write(ac, html, ec, null);
		ajax.addHtml(html.toString());
		ajax.send();
		return ac;
	}

}
